package com.pvdnc.systemdebugger.testclient;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.IBinder;
import android.os.TestLooperManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.system.ErrnoException;
import android.system.Os;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.pvdnc.systemdebugger.compacter.AuthProxy;
import com.pvdnc.systemdebugger.compacter.IAuth;
import com.pvdnc.systemdebugger.compacter.PBServiceManager;
import com.pvdnc.systemdebugger.compacter.PBServiceManagerProxy;
import com.pvdnc.systemdebugger.compacter.utils.AppContext;

import java.lang.reflect.Method;

public class TestMainActivity extends AppCompatActivity {
    private static final String TAG="TestMainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_main);
        Button cmdPATest=(Button)findViewById(R.id.cmdPATest);
        cmdPATest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PBServiceManagerProxy pbServiceManagerProxy = PBServiceManager.getSelf();
                IBinder aRemote = pbServiceManagerProxy.getService(IAuth.SELF_SERVICE_NAME);
                if (aRemote == null) {
                    Log.e(TAG, "remote auth service is null");
                    return;
                }
        /*AuthProxy authProxy=new AuthProxy(aRemote);
        int authResult= authProxy.request("testOpt");*/

                RPackageManager.validClass();
                TCompacter.remoteAuth();
                TCompacter.remotePolicy();
            }
        });

        final TextView lblInfo=(TextView)findViewById(R.id.lblInfo);
        Button cmdFASTest=(Button)findViewById(R.id.cmdFASTest);
        cmdFASTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Class<?> Zygote=Class.forName("com.android.internal.os.Zygote");
                    Method forkAndSpecialize=MethodUtils.getMethodByName(Zygote,"forkAndSpecialize").get(0);
                    String sig=VMHelper.getSignature(forkAndSpecialize);
                    Log.d(TAG,"forkAndSpecialize signature:\n"+sig);
                    lblInfo.setText(sig);
                    //(II[II[[IILjava/lang/String;Ljava/lang/String;[ILjava/lang/String;Ljava/lang/String;)I
                    //(II[II[[IILjava/lang/String;Ljava/lang/String;[ILjava/lang/String;Ljava/lang/String;)I
                    //(II[II[[IILjava/lang/String;IILjava/lang/String;[ILjava/lang/String;Ljava/lang/String;)I
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        Button cmdTRDTest=(Button)findViewById(R.id.cmdTRDTest);
        cmdTRDTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TripleResultDialog dialog=new TripleResultDialog(AppContext.getAppContext());
                dialog.setTitle("QuestionDialog");
                dialog.setMessage("开启TRDService");
                if(dialog.showDialog()==TripleResultDialog.RESULT_ALLOW){
                    Log.d(TAG,"attempt to start service:"+TRDService.class.getName());
                    Intent serviceIntent=new Intent(AppContext.getAppContext(),TRDService.class);
                    AppContext.getAppContext().startService(serviceIntent);
                }
            }
        });

        Button cmdVAExeCve=(Button)findViewById(R.id.cmdVAExeCve);
        cmdVAExeCve.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Os.execve("uname",new String[]{"-a"},Os.environ());
                } catch (ErrnoException e) {
                    e.printStackTrace();
                }
            }
        });

        Button cmdVAGetLocation=(Button)findViewById(R.id.cmdVAGetLocation);
        cmdVAGetLocation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LocationManager mLM= (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                @SuppressLint("MissingPermission")
                Location location= mLM.getLastKnownLocation("gps");
                Log.d(TAG,"last known location:"+ location.toString());
            }
        });

    }
}
